<?php

namespace common\modules\user\models\backend;

use common\widgets\ActiveForm;
use common\widgets\DateTimePicker;
use common\widgets\Form;
use common\widgets\Select2;
use common\widgets\SelectUserWidget;
use Yii;

/**
 * This is the form model class for model "User".
 */
class UserForm extends User
{

    /**
     * @inheritdoc
     */
    public $isNewRecord;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return parent::rules();
    }

    const SCENARIO_CREATE = 'admin-create';
    const SCENARIO_UPDATE = 'admin-update';

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        return [
            'admin-create' => [
                'id',
                'username',
                'email',
                'password_hash',
                'auth_key',
                'token',
                'group_id',
                'role',
                'status_id',
                'join_type',
                'password',
                'repeat_password',
                'created_at',
                'updated_at',
                'created_by',
                'updated_by',
            ],
            'admin-update' => [
                'id',
                'username',
                'email',
                'password_hash',
                'auth_key',
                'group_id',
                'token',
                'role',
                'status_id',
                'join_type',
                'password',
                'repeat_password',
                'created_at',
                'updated_at',
                'created_by',
                'updated_by',
            ],
        ];
    }

    /**
     * 返回当前表单的名称
     *
     * @return string
     */
    public function classLabelName()
    {
        return Yii::t('backend/user', 'User');
    }

    /**
     * 列表定义
     *
     * @param ActiveForm|Form $form
     * @param                 $model UserForm
     * @param mixed           $extra
     * @return array
     * @throws \yii\web\ServerErrorHttpException
     */
    public function formRows(&$form, $model, $extra)
    {
        return [

            [
                'columns'    => 4,
                'attributes' => [

                    'username' => [
                        'type' => Form::INPUT_TEXT,
                    ],

                    'email' => [
                        'type' => Form::INPUT_TEXT,
                    ],

                    'password' => [
                        'type' => Form::INPUT_PASSWORD,
                    ],
                    'repeat_password' => [
                        'type' => Form::INPUT_PASSWORD,
                    ],
                ],
            ],

            [
                'columns'    => 3,
                'attributes' => [

                    'role' => [
                        'type'        => Form::INPUT_WIDGET,
                        'widgetClass' => Select2::className(),
                        'options'     => [
                            'options'       => [
                                'multiple'    => true,
                                'placeholder' => Yii::t('backend/user', 'BACKEND_PROMPT_ROLE'),
                            ],
                            'pluginOptions' => [
                                'width' => '100%',
                            ],
                            'data' => self::getRoleArray(),
                        ],
                    ],

                    'group_id' => [
                        'type'        => Form::INPUT_WIDGET,
                        'widgetClass' => Select2::className(),
                        'options'     => [
                            'options'       => [
                                'placeholder' => Yii::t('backend/user', 'BACKEND_PROMPT_GROUP'),
                            ],
                            'pluginOptions' => [
                                'width' => '100%',
                            ],
                            'data' => GroupTree::getTreeList(),
                        ],
                    ],

                    'status_id' => [
                        'type'        => Form::INPUT_WIDGET,
                        'widgetClass' => Select2::className(),
                        'options'     => [
                            'data' => self::getStatusArray(),
                        ],
                    ],

                    'auth_key' => [
                        'type' => Form::INPUT_TEXT,
                    ],

                    'token' => [
                        'type' => Form::INPUT_TEXT,
                    ],

                    'join_type' => [
                        'type'        => Form::INPUT_WIDGET,
                        'widgetClass' => Select2::className(),
                        'options'     => [
                            'data' => self::getJoinTypeArray(),
                        ],
                    ],
                ],
            ],

            [
                'columns'    => 4,
                'attributes' => [
                    'created_at' => [
                        'type'        => Form::INPUT_WIDGET,
                        'widgetClass' => DateTimePicker::className(),
                        'options' => [
                            'type' => DateTimePicker::TYPE_INPUT,
                            'options' => ['disabled' => 'disabled'],
                        ],
                    ],

                    'updated_at' => [
                        'type'        => Form::INPUT_WIDGET,
                        'widgetClass' => DateTimePicker::className(),
                        'options' => [
                            'type' => DateTimePicker::TYPE_INPUT,
                            'options' => ['disabled' => 'disabled'],
                        ],
                    ],

                    'created_by' => [
                        'type' => Form::INPUT_WIDGET,
                        'widgetClass' => SelectUserWidget::className(),
                        'options' => [
                            'isStatic' => true,
                        ],
                    ],
                    'updated_by' => [
                        'type' => Form::INPUT_WIDGET,
                        'widgetClass' => SelectUserWidget::className(),
                        'options' => [
                            'isStatic' => true,
                        ],
                    ],
                ],
            ],
        ];
    }

}
